#ifndef SANGAME_BASE_DATA_POOL_H_
#define SANGAME_BASE_DATA_POOL_H_

#include "base/singleton.h"

namespace sangame {
namespace base {

template<class T>
class DataPool : public Singleton<DataPool<T>> {
public:
    boost::shared_ptr<T> GetData(typename T::Key& key) {
        DataMap::const_iterator it = data_pool_.find(key);
        if(it != data_pool_.end()) {
            boost::weak_ptr<T> weak_data_ptr = it->second;
            if(!weak_data_ptr.expired()) {
                return weak_data_ptr.lock();
            }
        }

        boost::shared_ptr<T> data_ptr = boost::make_shared<T>(key);
        boost::weak_ptr<T> weak_data_ptr = data_ptr;
        data_pool_.insert(boost::make_pair(key, weak_data_ptr));

        return pData;
    }

private:
    typedef boost::unordered_map<typename T::Key, boost::weak_ptr<T>, boost::hash<typename T::Key>> DataMap;
    DataMap data_pool_;

};

}
}

#endif // SANGAME_BASE_DATA_POOL_H_